<?php

namespace app\admin\model;
use think\Model;

/**
 * This is the model class for table "hxdc_project_action_records".
 *
 * @property int $id
 * @property int $action_id 活动id
 * @property int $member_id 用户id
 * @property string $nickname 用户名
 * @property string $mobile 手机号
 * @property int $created_at 报名时间
 * @property int $updated_at 审核时间
 * @property int $status 审核状态，0待审核；1通过，-1不通过
 */
//
class ProjectActionRecords extends Model
{
    public static function paramsNew() :array {
        return array(
//            'id' => '项目ID',
            'action_id' => '',
            'member_id' => '',
            'nickname' => '',
            'mobile' => '',
            'created_at' => time(),
            'updated_at' => time(),
            'status' => 1,
        );
    }
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hxdc_project_action_records';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['action_id', 'member_id', 'nickname', 'mobile'], 'required'],
            [['action_id', 'member_id', 'created_at', 'updated_at', 'status'], 'integer'],
            [['nickname', 'mobile'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'action_id' => '活动 ID',
            'member_id' => '用户 ID',
            'nickname' => '姓名',
            'mobile' => '手机号',
            'created_at' => '报名时间',
            'updated_at' => '审核时间',
            'status' => '状态',
        ];
    }
    /**
     * 关联会员
     */
    public function getMember()
    {
        return $this->hasOne(Member::class, ['id' => 'member_id']);
    }
    /**
     * 关联会员
     */
    public function getAction()
    {
        return $this->hasOne(ProjectAction::class, ['id' => 'action_id']);
    }

    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}
